package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.CALLED_ADD_METHOD;
import static com.printeverywhere.util.AdminConstants.ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITH_SEARCH_LIST_CRITERIA;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.Deposits;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchListCriteria;

public class TransactionsServiceDelegator {
	private static Logger logger = Logger
			.getLogger(TransactionsServiceDelegator.class);

	/**
	 * update the amount to deposited table.
	 *
	 * @param Deposits
	 *            the Depositss
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static void update(Transactions transactions) throws HibernateException {
		logger.info(AdminConstants.CALLED_UPDATE_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.update(transactions);
			transaction.commit();
			logger.info(AdminConstants.RECORD_UPDATED);
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_UPDATING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	public static Transactions get(int transctionId) throws HibernateException {
		logger.info("Called get method".intern());
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		Transactions deposits = new Transactions();
		try {
			Criteria criteria = session.createCriteria(Transactions.class).add(
					Expression.eq("TID", transctionId));
			List<Transactions> list = criteria.list();
			deposits = (list.size() > 0) ? list.get(0) : new Transactions();
			transaction.commit();
		} catch (HibernateException e) {
			logger.info("Error in getting the object for given categoryId "
					.intern()
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
		return deposits;
	}

	public static void add(Transactions transactions) throws HibernateException {
		logger.info(CALLED_ADD_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.save(transactions);
			transaction.commit();
			logger.info(AdminConstants.RECORD_ADDED);
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_ADDING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}
	@SuppressWarnings("unchecked")
	public static List<Transactions> getList() throws HibernateException {
		logger.info(GET_LIST_WITHOUT_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			List<Transactions> list = session.createCriteria(
					Transactions.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			logger.error(ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}
	public static List<Transactions> getList(
			SearchListCriteria searchCriteria) throws HibernateException {
		logger.info(GET_LIST_WITH_SEARCH_LIST_CRITERIA);
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Transactions.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object[] value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			if (((String) value[1]).equals(SearchListCriteria.EQ)) {
				criteria.add(Restrictions.eq(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.GE)) {
				criteria.add(Restrictions.ge(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.GT)) {
				criteria.add(Restrictions.gt(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.ILIKE)) {
				criteria.add(Restrictions.ilike(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.ISNOTNULL)) {
				criteria.add(Restrictions.isNotNull(key));
			} else if (((String) value[1]).equals(SearchListCriteria.ISNULL)) {
				criteria.add(Restrictions.isNull(key));
			} else if (((String) value[1]).equals(SearchListCriteria.LE)) {
				criteria.add(Restrictions.le(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.LIKE)) {
				criteria.add(Restrictions.like(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.LT)) {
				criteria.add(Restrictions.lt(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.NE)) {
				criteria.add(Restrictions.ne(key, value[0]));
			} else if (((String) value[1]).equals(SearchListCriteria.IN)) {
				criteria.add(Restrictions.in(key, (Object[]) value[0]));
			}
		}
		Transaction transaction = session.beginTransaction();
		List<Transactions> list = new ArrayList<Transactions>();
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			for (String keyexception : keys) {
				logger.info(keys);
			}
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}

}
